home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 1 / Amiga Tools.iso / egs-tools / egs_dev-disk / egsdemos / moreexamples / viewgad / viewgad.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-06  |  7.3 KB  |  191 lines

  1.                            /* View Gad.c
  2.                               Source code for View Gadgets. These are very
  3.                               handy little gadgets. This is only a small
  4.                               demo, there is more you can do. I don't
  5.                               believe this is documented by Viona yet,
  6.                               so look at include:egs/clib/gbview_protos.h
  7.                               for the functions.
  8.  
  9.                               Patrick Hager, Great Valley Products Inc.
  10.                               November 6, 1993.
  11.                             */
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include "/global/userwindow.h"
  15. #include "/global/egslibraries.h"
  16.  
  17. #include <clib/alib_protos.h>
  18. #include <clib/exec_protos.h>
  19. #include <egs/egs.h>
  20. #include <egs/clib/egs_protos.h>
  21. #include <egs/pragmas/egs_pragmas.h>
  22. #include <egs/egsintui.h>
  23. #include <egs/clib/egsintui_protos.h>
  24. #include <egs/pragmas/egsintui_pragmas.h>
  25. #include <egs/egsgadbox.h>
  26. #include <egs/clib/egsgadbox_protos.h>
  27. #include <egs/pragmas/egsgadbox_pragmas.h>
  28. #include <egs/egb/gbview.h>
  29. #include <egs/clib/gbview_protos.h>
  30. #include <egs/pragmas/gbview_pragmas.h>
  31.  
  32. #define VIEWGAD_ID 400
  33. #define ZOOM_BASE_ID 500      // Only one ID for a multi action.
  34.                               // 500 = first, 501 = second etc.
  35.  
  36. struct UserInputWindow Example_Request;
  37. struct EGB_ViewGadget *ViewGad; // It is very handy to keep a pointer to the
  38.                                 // View gad once we've created it, so we
  39.                                 // can modify it later.
  40. E_EBitMapPtr Example_BitMap; // This is the actual image we view through the
  41.                              // viewgad.
  42.  
  43. struct MsgPort *ExamplePort; // Our Port.
  44. WORD ViewGad_Zoom=0;         // Our current zoom factor.
  45. BYTE Quit_Example=0;         // Flag to quit the program.
  46.  
  47.  
  48.                       // Prototypes
  49. EB_GadBoxPtr Create_Example (UserInputWindowPtr UserWindow);
  50. void GadgetDown_Example (UserInputWindowPtr UserWindow, EI_GadgetPtr iaddress);
  51. void Redraw_Example (UserInputWindowPtr UserWindow);
  52. void Close_Example (UserInputWindowPtr UserWindow);
  53.  
  54. void main (void) {
  55.   FILE *fp;
  56.   WORD sizex, sizey;
  57.   EI_EIntuiMsgPtr Mesg;
  58.  
  59.   if (OpenEGSLibraries ()) {
  60.     if (ExamplePort = CreatePort (0,0)) {
  61.       if (fp = fopen ("picture.raw","rb")) {
  62.         fread ((char *)&sizex,2,1,fp);
  63.         fread ((char *)&sizey,2,1,fp);
  64.         if (Example_BitMap = E_AllocBitMap (sizex,sizey,24,E_PIXELMAP,0,NULL)) {
  65.                               // Load picture to our bitmap.
  66.                               // ALLWAYS lock your memory before accesing
  67.                               // directly.
  68.           ++Example_BitMap->Lock;
  69.           fread ((char *)Example_BitMap->Plane,4,sizex*sizey,fp);
  70.           --Example_BitMap->Lock;
  71.  
  72.           Init_UserWindow (&Example_Request, "Example Window",WINDOW_NONMODAL,NULL,
  73.                            ExamplePort);
  74.           Example_Request.Create     = Create_Example;
  75.           Example_Request.GadgetDown = GadgetDown_Example;
  76.           Example_Request.Redraw     = Redraw_Example;
  77.           Example_Request.Close      = Close_Example;
  78.  
  79.           Example_Request.IDCMPFlags = EI_iCLOSEWINDOW | EI_iSIZEVERIFY |
  80.                                        EI_iNEWSIZE |
  81.                                        EI_iGADGETDOWN;
  82.           Example_Request.Flags =      EI_GIMMEZEROZERO| EI_SIZEBBOTTOM| EI_SIMPLE_REFRESH;
  83.           Example_Request.SysGadgets = (EI_WINDOWCLOSE)|(EI_WINDOWSIZE)|EI_WINDOWDRAG;
  84.  
  85.           if (Open_UserWindow (&Example_Request,-1,-1)) {
  86.             EGB_ModifyBMZoomViewGadget (Example_Request.Window,ViewGad,
  87.                                         Example_BitMap,0,0,0);
  88.             while (!Quit_Example) {
  89.               WaitPort (ExamplePort);
  90.               while (Mesg=(struct EI_EIntuiMsg *)GetMsg (ExamplePort)) {
  91.                 if ( ((WindowInfoPtr)Mesg->IDCMPWindow->UserData)->EventHandler) {
  92.                   ((WindowInfoPtr)Mesg->IDCMPWindow->UserData)->EventHandler (Mesg);
  93.                 }
  94.               }
  95.             }
  96.             Close_All_UserWindows ();
  97.           }
  98.           else
  99.             printf ("Couldn't open my window!\n");
  100.           if (Example_BitMap)
  101.             E_DisposeBitMap (Example_BitMap);
  102.         }
  103.         else
  104.           printf ("Couldn't alloc the memory for my image!\n");
  105.       }
  106.       else
  107.         printf ("Couldn't open file pointer for my image!\n"
  108.                 "Picture.raw must be in the same directory as me.\n");
  109.       DeletePort (ExamplePort);
  110.      }
  111.      else
  112.        printf ("Couldn't open my Port!\n");
  113.   }
  114.   CloseEGSLibraries ();  // after the bracket cause what if I opened 3 but
  115.                          // couldn't find the rest, still need to close the 3.
  116. }
  117.  
  118. /*************************************
  119.    PRIVATE:  Create_Example.
  120.              This routine is called by the UserWindow routines.
  121.              It passes back a GadBoxPtr to the gadgets wanted.
  122. *************************************/
  123. EB_GadBoxPtr Create_Example (UserInputWindowPtr UserWindow) {
  124.   EB_GadBoxPtr root, box;
  125.   EB_GadContext gadcon;
  126.   EB_StrArray names = {"Zoom in", "Zoom out",NULL};
  127.  
  128.   gadcon = UserWindow->GadCon;
  129.  
  130.   root = EB_CreateVertiBox (gadcon);
  131.   box= EGB_CreateViewGadget (gadcon,32,4000,32,4000,
  132.                              EGB_ViewScrollRight|EGB_ViewScrollBottom,
  133.                              VIEWGAD_ID);
  134.   ViewGad = (EGB_ViewGadPtr)box->Render.Gad;
  135.   EB_AddLastSon (root,EB_CreateGroupBorder (gadcon,box,EB_FILL_ALL,
  136.                                             " View Gadget "));
  137.   EB_AddLastSon (root,EB_CreateVertiFill (gadcon,0,0));
  138.   EB_AddLastSon (root,EB_CreateMultiAction (gadcon,&names,ZOOM_BASE_ID,EB_FILL_ALL));
  139.   return root;
  140. }
  141.  
  142. /**************************************
  143.     PRIVATE:  GadgetDown_Example.
  144.               This routine handles the gadget down events passed to the
  145.               Example Control Window.
  146. **************************************/
  147. void GadgetDown_Example (UserInputWindowPtr UserWindow, EI_GadgetPtr iaddress) {
  148.  
  149.   switch (iaddress->GadgetID) {
  150.     case ZOOM_BASE_ID:   // Zoom in
  151.       ++ViewGad_Zoom;
  152.       Redraw_Example (UserWindow);
  153.     break;
  154.     case ZOOM_BASE_ID+1: // Zoom out
  155.       --ViewGad_Zoom;
  156.       Redraw_Example (UserWindow);
  157.     break;
  158.   }
  159.  
  160. }
  161. /*************************************
  162.    PRIVATE:  Close_Example.
  163.              This routine CANNOT be used to close the Example
  164.              Control Window!!! It is called by the UserWindow
  165.              routines to allow this window to clean up any
  166.              allocated memory.
  167.              To Close the Example Control Window, call
  168.              CloseDown_Example ();
  169. *************************************/
  170. void Close_Example (UserInputWindowPtr UserWindow) {
  171.  
  172.   Quit_Example=1;
  173. }
  174.  
  175. /**************************************
  176.     PUBLIC:  Redraw_Example.
  177.              This routine handles redraw events to the Example
  178.              Control Window. Called by UserWindow routines, and
  179.              could be called by other routines as well.
  180.              Also called on an EI_iREFRESHWINDOW message.
  181. **************************************/
  182. void Redraw_Example (UserInputWindowPtr UserWindow) {
  183.  
  184.                        // We need to reset the View Gad on a NewSize
  185.                        // Message. Redraw is called on a newsize, so we'll
  186.                        // do it here.
  187.  
  188.   EGB_ModifyBMZoomViewGadget (Example_Request.Window,ViewGad,
  189.                               Example_BitMap,0,0,ViewGad_Zoom);
  190. }
  191.